import type { RequestParams } from "@/api";
import type { FileRouteTypes } from "@/routeTree.gen";
import type {
	BaseSearchModel,
	ListModel,
	ListSearchModel,
	PagedListSearchModel,
} from "@/types";
import type { ModalProps, TableColumnProps } from "@arco-design/web-react";
import type { FC, ReactNode } from "react";

export type StProps<
	TModel = unknown,
	TSearch extends BaseSearchModel | undefined = undefined,
> = {
	data:
		| TModel[]
		| ListModel<TModel>
		| ((
				req: ListSearchModel<TSearch> | PagedListSearchModel<TSearch>,
				params?: RequestParams,
		  ) => Promise<TModel[] | Partial<ListModel<TModel>>>);
	columns: StColumnsType<TModel>;
	searchModel?: TSearch;
	rowKey?: string | ((record: TModel) => string);
	pageSize?: number;
	hidePagination?: boolean;
	queryKey?: string;
	rowSelection?: "checkbox" | "radio";
	selectedKeys?: (string | number)[];
	onSelectedKeysChange?: (selectedKeys: (string | number)[]) => void;
};

export type StRenderFunction<TModel, TReturn> = (record: TModel) => TReturn;

export type StColumnButtonOptions<TModel> = {
	text?: string | StRenderFunction<TModel, string>;
	icon?: StRenderFunction<TModel, ReactNode>;
	type?: "none" | "del" | "modal" | "link";
	click?:
		| "reload"
		| "load"
		| StRenderFunction<TModel, undefined | FileRouteTypes["to"]>
		| StRenderFunction<TModel, void>;
	pop?: boolean | string;
	iif?: StRenderFunction<TModel, boolean>;
	iifBehavior?: "hide" | "disabled";
	tooltip?: string;
	acl?: string;
	modal?: StColumnButtonModalOptions<TModel>;
};

export type StColumnButtonModalOptions<TModel> = {
	component: FC;
	params:
		| Record<string, unknown>
		| StRenderFunction<TModel, Record<string, unknown>>;
	title: string | StRenderFunction<TModel, string>;
	width?: number;
	modalConfig?: Partial<Omit<ModalProps, "title" | "titleAlign" | "width">>;
};

export type StColumnType<TModel> = Omit<TableColumnProps<TModel>, "width"> &
	Partial<{
		type: "no" | "link" | "img" | "date" | "button";
		width: number;
		buttons: StColumnButtonOptions<TModel>[];
		click: StRenderFunction<TModel, undefined | FileRouteTypes["to"]>;
		dateFormat: string;
		iif: (column: StColumnType<TModel>) => boolean;
		sort: string | boolean;
		imgSize: number;
		imgWidth: number;
		imgHeight: number;
	}>;

export type StColumnsType<TModel> = StColumnType<TModel>[];

export interface StRef<TModel> {
	reload: () => void;
	load: (pageIndex: number) => void;
	getSelectedRows: () => TModel[];
	data: TModel[];
	rowKey: string;
}
